package com.example.myfirstgame.helper;

public class Rasterizer {

	private int mCanvasHeight;
	private int mCanvasWidth;
	private int mRPInWidth;
	private int mRPInHeight;
	private int mRPLengthSize;
	private int mRPBorderSizeHeight;
	private int mRPBorderSizeWidth;

	public Rasterizer(int pCanvasHeight, int pCanvasWidth) {
		mCanvasHeight = Math.abs(pCanvasHeight);
		mCanvasWidth = Math.abs(pCanvasWidth);
	}

	public void initRastering(float pAspectRatio, int pNumOfRasterPointsWidth) {
		mRPInWidth = pNumOfRasterPointsWidth;
		mRPInHeight = (int) (pAspectRatio * (float) mRPInWidth);

		int lPossibleRPLengthSize = (int) ((float) mCanvasWidth / (float) pNumOfRasterPointsWidth);

		while(mCanvasHeight <= mRPInHeight * lPossibleRPLengthSize) {
			lPossibleRPLengthSize -= 1;
		}
		mRPLengthSize = lPossibleRPLengthSize;
		mRPBorderSizeWidth = (int) ((float) (mCanvasWidth - (mRPInWidth * mRPLengthSize)) / 2);
		mRPBorderSizeHeight = (int) ((float) (mCanvasHeight - (mRPInHeight * mRPLengthSize)) / 2);
	};

	public Raster generateRaster() {
		Raster lRaster = new Raster(mRPLengthSize);
		int lCounterHeight = 0;
		int lCounterWidth = 0;
		for (int actHeight = mRPBorderSizeHeight; actHeight <= mCanvasHeight
				- mRPBorderSizeHeight; actHeight += mRPLengthSize) {
			lCounterWidth = 0;
			for (int actWidth = mRPBorderSizeWidth; actWidth < mCanvasWidth
					- mRPBorderSizeWidth; actWidth += mRPLengthSize) {
				Coordinate lRPCoordinate = new Coordinate(actWidth, actHeight);
				RasterPoint lRasterPoint = new RasterPoint(lRPCoordinate, String.valueOf(lCounterWidth), String.valueOf(lCounterHeight));
				lRaster.addRasterPoint(lRasterPoint);
				lCounterWidth++;
			}
			lCounterHeight++;
		}
		int indexHeigthWidth[] = new int[2];
		indexHeigthWidth[0] = lCounterHeight;
		indexHeigthWidth[1] = lCounterWidth;
		lRaster.setmMaxHeightAndWidth(indexHeigthWidth);
		return lRaster;
	}

}
